package com.android.publicModule.model.img;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.android.publicModule.system.SystemInfo;
import com.android.publicModule.tools.Tools;
import com.muge.cartoon.naruto.R;

import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class BitmapDrawableManage {

	private Context mContext;
	
	public BitmapDrawableManage(Context context) {
		mContext = context.getApplicationContext();
	}
	
	public Drawable createDrawable(String path) {
		Drawable drawable = null;
		if(path!=null&&path.length()>0)
		{
			drawable = Drawable.createFromPath(path);
		}
		return drawable;
	}
	
	public Drawable createDrawable(int resourceId) {
		Drawable drawable = null;
		try {
			Resources res = mContext.getResources();
			if(res!=null)
			{
				drawable = res.getDrawable(resourceId);
			}
		} catch(NotFoundException e) {
			
		} catch(Exception e) {
			
		}
		return drawable;
	}
	
	public Drawable createDrawable(int resourceId,int density) {
		Drawable drawable = null;
		try {
			Resources res = mContext.getResources();
			if(res!=null)
			{
				drawable = res.getDrawableForDensity(resourceId, density);
			}
		} catch(NotFoundException e) {
			
		} catch(Exception e) {
			
		}
		return drawable;
	}
	
	public Drawable createDrawableByUrl(String url,String type) {
		Drawable drawable = null;
		if(url!=null&&url.length()>0)
		{
			if(url.startsWith("/"))
			{
				drawable = createDrawable(url);
			}
			else
			{
				if(type==null||type.length()>0)
				{
					type = ".jpg";
				}
				else if(!type.startsWith("."))
				{
					type = "."+type;
				}
				String imgName = Tools.getUniqueString(url)+type;
				String path = Tools.getExistsFilePath(mContext, SystemInfo.DEFAULT_ICON_PATH+"/"+imgName);
				if(path!=null&&path.length()>0)
				{
					drawable = createDrawable(path);
				}
				else
				{
					try {
						URL mUrl = new URL(url);
						HttpURLConnection urlConn;
						urlConn = (HttpURLConnection) mUrl.openConnection();
						InputStream is = urlConn.getInputStream();
						if(is!=null)
						{
							drawable = createDrawableFromBitmap(BitmapFactory.decodeStream(is));
						}
					} catch (MalformedURLException e) {
						
					} catch (IOException e) {
						
					}
				}
			}
			if(drawable==null)
			{
				drawable = createDrawable(R.drawable.icon);
			}
		}
		return drawable;
	}
	
	public ShortcutIconResource createShortcutIconResource(int resourceId) {
		return Intent.ShortcutIconResource.fromContext(mContext, resourceId);
	}
	
	public Bitmap createBitmap(int resourceId) {
		Bitmap bitmap = null;
		Resources res = mContext.getResources();
		if(res!=null)
		{
			try {
				bitmap = BitmapFactory.decodeResource(res, resourceId);
			} catch(NotFoundException e) {
				
			} catch(Exception e) {
				
			}
		}
		return bitmap;
	}
	
	public Bitmap createBitmap(int resourceId,Options opts) {
		Bitmap bitmap = null;
		Resources res = mContext.getResources();
		if(res!=null)
		{
			try {
				bitmap = BitmapFactory.decodeResource(res, resourceId, opts);
			} catch(NotFoundException e) {
				
			} catch(Exception e) {
				
			}
		}
		return bitmap;
	}
	
	public Bitmap createBitmap(String path) {
		Bitmap bitmap = null;
		if(path!=null&&path.length()>0)
		{
			bitmap = BitmapFactory.decodeFile(path);
		}
		return bitmap;
	}
	
	public Bitmap createBitmap(String path,Options opts) {
		Bitmap bitmap = null;
		if(path!=null&&path.length()>0)
		{
			bitmap = BitmapFactory.decodeFile(path, opts);
		}
		return bitmap;
	}
	
	public Bitmap createBitmapFromDrawable(Drawable drawable) {
		Bitmap bitmap = null;
		if(drawable!=null)
		{
			bitmap = ((BitmapDrawable)drawable).getBitmap();
		}
		return bitmap;
	}
	
	public Drawable createDrawableFromBitmap(Bitmap bitmap) {
		BitmapDrawable bitmapDrawable = null;
		if(bitmap!=null)
		{
			bitmapDrawable = new BitmapDrawable(bitmap);
		}
		return bitmapDrawable;
	}
	
	public static Bitmap convertBytesToBimap(byte[] b) {
		Bitmap bitmap = null;
		if (b!=null&&b.length>0) {
			bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
		}
		return bitmap;
	}
	
	public static byte[] convertBitmapToBytes(Bitmap bm) {
		byte[] byteArr = null;
		if(bm!=null)
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
			byteArr = baos.toByteArray();
		}
		return byteArr;
	}
	
}
